﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using SageKing.Blazor.OpenIdService.Components.Base;
using SageKing.Blazor.OpenIdService.Components.Shared;
using SageKing.Core.Service;
using SageKing.OpenIddict.Services.BaseService;

namespace SageKing.Blazor.OpenIdService.Components.Application.Pages.Manage.Components;

public partial class IndexLeft
{
    #region 参数
    /// <summary>
    /// 动态显示的列名
    /// </summary>
    public override List<string> ColumnsByNamesShow { get; set; } = ["DisplayName", "Type", "Status", "OrderNo", "Remark", "CreateUserName"];

    #endregion

    #region Inject 相关 
    [Inject]
    public ApplicationApiBaseService _dataService { get; set; }
    #endregion

    #region init system
    protected override async Task OnInitializedAsync()
    {
        try
        {
            TemplateName = TabTitle = "应用管理";
            await base.OnInitializedAsync();
            initModal();
            await Refresh(false);
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }
    public void initModal()
    {
        var tmpColConfig = GetDefaultColConfig();
        modal = new TableSetModal<ApplicationViewModel>(this, tmpColConfig);
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        try
        {
            await base.OnAfterRenderAsync(firstRender);

        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }

    #endregion

    #region 查询条件相关
    #region OnSearch 

    public override async Task OnSearch(PaginationEventArgs args)
    {
        tableLoading = true;

        pageargs.Page = args.Page;
        pageargs.PageSize = args.PageSize;


        List<(bool, Expression<Func<OpenIddictEntityFrameworkCoreApplication, bool>>)> tmpWhereIf = new() {
                (searchModel.DisplayName.IsNotNullOrWhiteSpace(),a=>a.DisplayName.Contains(searchModel.DisplayName))
            };

        var page = new PageBaseInput() { Page = pageargs.Page, PageSize = pageargs.PageSize };

        pageData = await _dataService.GetPage(page, tmpWhereIf,
            a => a.Id, false);

        tableLoading = false;
        await Task.CompletedTask;
    }
    #endregion
    #endregion

    #region Table DefinitionColumns

    /// <summary>
    /// 动态列定义处理
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="column"></param>
    /// <returns></returns>

    public override void DefinitionColumns(string propertyName, IFieldColumn column)
    {
        var getColDefine = DefinitionColumnsBase(propertyName, column);
        //to do
    }
    #endregion


    private async Task onClickTableSet(MouseEventArgs args)
    {
        modal.Visible = true;
        await Task.CompletedTask;
    }
}
